package com.mentorgen.tools.util.profile.bundle;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import com.mentorgen.tools.profile.instrument.memory.MemoryCounter;

/**
 *
 * @author slim ouertani
 */
class LoggerProxy implements InvocationHandler, Serializable {

	private static final long serialVersionUID = 6449515915905463388L;

	private ServiceReference serviceReference;
    private BundleContext bundleContext;

    public LoggerProxy(BundleContext bundleContext,
            ServiceReference serviceReference) {
        this.serviceReference = serviceReference;
        this.bundleContext = bundleContext;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        int totalIndata = 0;
        if (args != null)
	        for (Object object : args) {
	        	totalIndata += MemoryCounter.estimate(object);
	        }
        Object invoke = method.invoke(bundleContext.getService(serviceReference),
                args);

//        BundleBandwidthCollection.getInstance().getSrvContainer(
//        		serviceReference).addMethodDataExchng(method.getName(), totalIndata, MemoryCounter.estimate(invoke));
        
        return invoke;
    }
}
